define(['app'],function(app){
    app.controller('MainCtrl' ,['$scope','$rootScope','$window', '$timeout', '$http','$q','$location','systemService','localStorageService',
       function ($scope,$rootScope, $window, $timeout,$http, $q,$location,systemService,localStorageService) {	
        
        //代码书写步骤
        //1.用【$scope】定义公共类model,和页面绑定的model
        //2.用【var】定义controller内部用的变量
        //3.用【$scope】和【var】定义方法，注意区分两者定义的方法的区别
        //4.最后定义【init】初始化方法，并调用，进行页面的初始化加载
        //定义规则，按可用性分忧优先级，公共的在前，private的在后

    	//初始化 左侧菜单栏tree
    	$scope.treeData =[];
        $scope.dashboardModel = {
            isActiveMenu:'main',//记录treemenu当前选择项
            skinerToggleActive:false
        };

        //初始化user信息
        $rootScope.user = {
        	name: '',
            token:''
        };
        
        //侧边状态按钮切换
        $scope.autoLeftNav =function(){
            if ($('.left-sidebar').is('.active')) {
                $('.tpl-content-wrapper').removeClass('active');
                $('.left-sidebar').removeClass('active');
            } else {
                $('.left-sidebar').addClass('active');
                $('.tpl-content-wrapper').addClass('active');
            }
        };     
        //页面resize  
        $(window).resize(function() {
            if ($(window).width() < 1024) {
                $('.left-sidebar').addClass('active');
            } else {
                $('.left-sidebar').removeClass('active');
            }
        });

        /**
         * 退出登录
         * 
         * @return {[type]} [description]
         */
        $scope.logoutFun = function(){
            // console.log("登出");
            systemService.logoutFun().then(
                function(response){
                    if(response.status == 200){
                        $rootScope.user.name='';
                        $rootScope.user.token ='';
                        localStorageService.setName("");
                        localStorageService.setToken("");
                    }else{
                        alert("退出失败");
                    }
                },
                function(code){

                }
            );
           
        };

        /**
         * 切换按钮选中状态
         * 
         * @param  {[type]} tree [点击当前行的tree]
         * @return {[type]}      [description]
         */
        $scope.toggleActiveMenu = function(tree){
            $scope.dashboardModel.isActiveMenu = tree.id;
        };

        //加载tree json 数据
        var loadMenuTree = function(){
            $http({
                 method:'GET',
                 url:'app/data/tree/menuTree.json',
                 dataType: "json",
                 contentType:'application/json; charset=UTF-8'
             })
             .success(function(response){
                $scope.treeData = response.rows;//将json值赋给model变量
             })
             .error(function(e, code){
               return false;
             });
        }

        //获取session信息
        var getUserInfo = function(){
            var returnName =  localStorageService.getName();
            return returnName;
        }

        var init = function () {
            $scope.dashboardModel.isActiveMenu = 'main';//左侧菜单按钮赋初值
            loadMenuTree();//加载treejson
            $rootScope.user.name = getUserInfo();//读取用户信息
        };

        init();
    }]);
});